package cn.tedu.travelsystem.base.response;

import cn.tedu.travelsystem.carpool.pojo.entity.Carpool;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class JsonResult {
    private Integer state;
    private String message;
    private Object data;

    public JsonResult(StatusCode statusCode) {
        this.state = statusCode.getState();
        this.message = statusCode.getMessage();
    }

    public JsonResult(StatusCode statusCode, Object data) {
        this.state = statusCode.getState();
        this.message = statusCode.getMessage();
        this.data = data;
    }

    /** 操作成功，有返回数据 */
    public static JsonResult ok(Object data) {
        return new JsonResult(StatusCode.OPERATION_SUCCESS, data);
    }

    /** 操作成功，无返回数据 */
    public static JsonResult ok() {
        return ok(null);
    }

    /** 操作失败，自定义消息 */
    public static JsonResult fail(String message) {
        JsonResult result = new JsonResult();
        result.setState(400); // 客户端错误
        result.setMessage(message);
        return result;
    }

    /** 操作异常（服务器错误） */
    public static JsonResult error(String message) {
        JsonResult result = new JsonResult();
        result.setState(500); // 服务器错误
        result.setMessage(message);
        return result;
    }

    /** 特定方法使用：返回拼车匹配列表 */
    public static JsonResult ok(String msg, List<Carpool> matchingCarpools) {
        return new JsonResult(StatusCode.OPERATION_SUCCESS, matchingCarpools);
    }
}
